package com.suncky.example.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.google.gson.internal.bind.TypeAdapters
import com.google.gson.reflect.TypeToken
import com.suncky.example.R
import com.suncky.example.bean.Boundary
import com.suncky.example.bean.LatLng
import com.suncky.example.bean.LatLngs
import com.suncky.example.bean.Person
import com.suncky.example.databinding.ActivityGsonUtilsBinding
import com.suncky.frame.base.BaseDataBindingActivity
import com.suncky.frame.utils.FileUtil
import com.suncky.frame.utils.GsonUtils
import com.suncky.frame.utils.GsonUtils.ListTypeAdapter

class GsonUtilsActivity:BaseDataBindingActivity<ActivityGsonUtilsBinding>() {

    companion object {

        fun start(context: Context) {
            val starter = Intent(context, GsonUtilsActivity::class.java)
            if (context !is Activity) {
                starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(starter)
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_gson_utils
    }

//    private val jsonData="{\"name\":\"李平儿\",\"age\":26,\"gender\":\"女\",\"tel\":\"15652098198\",\"family\":[\"叶孤寒\",\"秋凝霜\",\"荆无命\",\"费空行\",],\"friends\":\"程遥, 幽明, 万里行, 宁玉莲\"}"
    private val jsonData="{\"name\":\"李平儿\",\"age\":26,\"gender\":\"女\",\"tel\":\"15652098198\"," +
        "\"family\":[\"叶孤寒\",\"秋凝霜\",\"荆无命\",\"费空行\"]," +
//        "\"family\":\"叶孤寒, 秋凝霜, 荆无命, 费空行\"," +
        "\"students\":[{\"name\":\"汪洋\",\"age\":26,\"gender\":\"女\",\"tel\":\"15652098198\",\"family\":[\"叶孤寒\",\"秋凝霜\",\"荆无命\",\"费空行\"],\"students\":[],\"friends\":[\"程遥\",\"幽明\",\"万里行\",\"宁玉莲\"],\"numbers\":[3.0,4.0,5.0,6.0,7.0,8.0]},{\"name\":\"费通\",\"age\":26,\"gender\":\"女\",\"tel\":\"15652098198\",\"family\":[\"叶孤寒\",\"秋凝霜\",\"荆无命\",\"费空行\"],\"students\":[],\"friends\":\"叶孤寒, 秋凝霜, 荆无命, 费空行\",\"numbers\":[23.0,45.0,56.0,67.0,78.0,89.0]}],"+
        "\"friends\":[\"程遥\",\"幽明\",\"万里行\",\"宁玉莲\"],\"numbers\":[1.0,2.0,3.0,4.0,5.0]}"

    private val person = Person(
        name = "李平儿",
        age = 26,
        gender = "女",
        tel = "15652098198",
        family = listOf("叶孤寒", "秋凝霜", "荆无命", "费空行"),
        students = listOf(
            Person(
                name = "李平儿",
                age = 26,
                gender = "女",
                tel = "15652098198",
                family = listOf("叶孤寒", "秋凝霜", "荆无命", "费空行"),
                students = listOf(),
                friends = "汤剑",
                numbers = listOf(3,4,5,6,7)
            ),Person(
                name = "李平儿",
                age = 26,
                gender = "女",
                tel = "15652098198",
                family = listOf("叶孤寒", "秋凝霜", "荆无命", "费空行"),
                students = listOf(),
                friends = "夏风",
                numbers = listOf(12,23,34,45,56)
            ),
        ),
        friends = "程遥, 幽明, 万里行, 宁玉莲",
        numbers = listOf(1,2,3,4,5)
    )

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        setTitle("GsonUtils")
    }

    override fun initData() {
        super.initData()
        val latlngJson = FileUtil.readFileToString(assets.open("411202.txt"))
        val myBoundary = GsonUtils.jsonToNestedParamTypeObject(
            latlngJson,
            Boundary::class.java,
            LatLngs::class.java,
            List::class.java,
            List::class.java,
            LatLng::class.java
        )
        val arrayJson = GsonUtils.arrayToJson(arrayOf(person,person,person))
        val listJson = GsonUtils.listToJson(listOf(person,person))
        val person = GsonUtils.jsonToObject(Person::class.java, jsonData)
        binding.person = person
        binding.boundary = myBoundary
    }
}